import * as Cesium from "cesium";
import initViewer from "@/initViewer.js";
import ContourLineAlgorithm from "@/plugins/ContourLineAlgorithm.js";
// import ContourLineAlgorithmES from "./ContourLineAlgorithm.min.js";
import ContourLineAlgorithmES from '../libs/ContourLineAlgorithm.min.js';
import ContourLineAlgorithmMinify from "../libs/ContourLineAlgorithm.min.js";
import "./style.css";


const loadBaseLayers = async (viewer) => {
  //加载天地图
  //将 https://t0.tianditu.gov.cn/img_w/wmts 替换成 http://172.16.0.100:5000/img_w/wmts
  viewer.imageryLayers.addImageryProvider(
    new Cesium.UrlTemplateImageryProvider({
      url: "http://t7.tianditu.gov.cn/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=aef1277d283beb91a608f2971d735fb2",
      // url: 'http://localhost:12018/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=aef1277d283beb91a608f2971d735fb2',
    })
  );
  viewer.imageryLayers.addImageryProvider(
    new Cesium.UrlTemplateImageryProvider({
      url: "http://t7.tianditu.gov.cn/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=aef1277d283beb91a608f2971d735fb2",
      // url: 'http://localhost:12018/DataServer?T=cia_w&x={x}&y={y}&l={z}&tk=aef1277d283beb91a608f2971d735fb2',
    })
  );
  const terrainProvider =
    await Cesium.ArcGISTiledElevationTerrainProvider.fromUrl(
      "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer",
      {
        token:
          "KED1aF_I4UzXOHy3BnhwyBHU4l5oY6rO6walkmHoYqGp4XyIWUd5YZUC1ZrLAzvV40pR6gBXQayh0eFA8m6vPg..",
        requestVertexNormals: true,
      }
    );
  viewer.terrainProvider = terrainProvider;
};

const viewer = initViewer("viewerContainer", { removeAll: true });
console.log(viewer);
loadBaseLayers(viewer);

const lnglats = [
  {
    longitude: 103.6579800436033,
    latitude: 33.79665491134846,
    height: 2552.231031077119,
  },
  {
    longitude: 103.75973884018468,
    latitude: 33.808506629959844,
    height: 2405.915399665163,
  },
  {
    longitude: 103.77093904871165,
    latitude: 33.72008203627272,
    height: 3527.005135728514,
  },
  {
    longitude: 103.69359331142391,
    latitude: 33.70296790308893,
    height: 4095.863733524433,
  },
  {
    longitude: 103.64790329366203,
    latitude: 33.736805226798005,
    height: 3837.649787589244,
  },
];
const positions = lnglats.map((lnglat) =>
  Cesium.Cartesian3.fromDegrees(
    lnglat.longitude,
    lnglat.latitude,
    lnglat.height
  )
);

//rectangle
const rectangle = Cesium.Rectangle.fromCartesianArray(
  positions,
  Cesium.Ellipsoid.WGS84
);
const center = Cesium.Rectangle.center(rectangle);
const caresianCenter = Cesium.Cartesian3.fromDegrees(
  Cesium.Math.toDegrees(center.longitude),
  Cesium.Math.toDegrees(center.latitude),
  15000
);
viewer.camera.flyTo({
  destination: caresianCenter,
});

//collection
const collection = new Cesium.PrimitiveCollection({
  show: true,
  destroyPrimitives: true,
});
viewer.scene.primitives.add(collection);

//ContourLineAlgorithm-[ContourLineAlgorithm]、[ContourLineAlgorithmES]、[ContourLineAlgorithmMinify]
const algorithm = new ContourLineAlgorithm(viewer, {
  spacing: 50,
  color: Cesium.Color.fromRandom(),
  granularity: Math.PI / Math.pow(2, 11) / 64.0,
});
console.log("algorithm::", algorithm);

const executeBtn = document.getElementById("execute");
executeBtn.onclick = async () => {
  console.log("click::", positions);
  //修改颜色
  algorithm.color = Cesium.Color.fromRandom();
  algorithm.spacing = 100;//Math.random() * 100;
  console.log("doAlgorithm1::", algorithm);

  //执行分析--[重名问题]
  algorithm
    .doAlgorithm(positions)
    .then((analysisResult) => {
      console.log("analysisResult::", analysisResult);
      const contourLineTable = analysisResult.contourLineTable,
        contourLines = analysisResult.contourLines;
      //渲染结果
      algorithm.renderContourLines(contourLines, contourLineTable, {
        collection: collection,
      });
    })
    .catch((err) => {
      console.log(err);
    });
};

const clearBtn = document.getElementById("clear");
clearBtn.onclick = function () {
  collection.removeAll();
};
